<template>
  <PageLayout class="form-container">
    <a-card>
      <div class="form-title">
        {{ route.query.id ? "编辑" : "新增" }}固件版本
      </div>
      <div class="form-box w-600">
        <a-form
          :model="formState"
          :rules="rules"
          ref="formRef"
          layout="Vertical"
          :labelCol="{ style: { width: '120px' } }"
        >
          <a-form-item label="固件名称" name="type" required>
            <a-select
              v-model:value="formState.type"
              style="width: 100%"
              placeholder="请选择设备类型"
              :options="[
                {
                  label: '主控板',
                  value: '0',
                },
                {
                  label: 'WIFI模块',
                  value: '1',
                },
              ]"
            ></a-select>
          </a-form-item>

          <a-form-item label="版本名称" name="name" required>
            <a-input
              v-model:value="formState.name"
              placeholder="请输入版本名称"
              :maxlength="50"
            />
          </a-form-item>
          <a-form-item label="版本号" name="version" required>
            <a-input
              v-model:value="formState.version"
              placeholder="请输入版本号"
              :maxlength="50"
            />
          </a-form-item>
          <a-form-item label="版本描述">
            <a-textarea
              v-model:value="formState.remark"
              placeholder="请输入"
              :maxlength="300"
            />
          </a-form-item>

          <a-form-item label="固件源文件" name="fileUrl" required>
            <!-- <UploadImg v-model:fileUrl="formState.fileUrl"></UploadImg> -->
            <UploadFile
              ref="uploadRef"
              :fileNum="1"
              :show-upload-list="{ showDownloadIcon: false }"
              label-name="上传固件源文件"
              :multiple="false"
              @change="uploadChange"
            ></UploadFile>
          </a-form-item>

          <a-button
            type="primary"
            class="float-right"
            style="width: 100px"
            @click="save"
            >确定</a-button
          >
        </a-form>
      </div>
    </a-card>
  </PageLayout>
</template>

<script setup>
import {
  postFirmwareAdd,
  postFirmwareUpdate,
  postFirmwareDetail,
} from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { message } from "ant-design-vue";
import { onMounted } from "vue";
const storeCommonEnum = useStoreCommonEnum();

const route = useRoute();
const router = useRouter();
const formRef = ref(null);

const formState = reactive({
  id: "",
  type: undefined,
  name: "",
  version: "",
  remark: "",
  fileUrl: "",
});
const deptOptions = ref([]);

onMounted(async () => {
  if (route.query.id) {
    postFirmwareDetail({ id: route.query.id }).then((res) => {
      for (let key in formState) {
        formState[key] = res.data[key];
      }
    });
  }
});

const rules = {
  title: [{ required: true, message: "请输入标题", trigger: "blur" }],
  fileUrl: [{ required: true, message: "请上传固件源文件" }],
  // linkUrl: [{ required: true, message: "请输入链接", trigger: "blur" }],
  // deptId: [{ required: true, message: "请选择部门", trigger: "blur" }],
};

const uploadChange = (file) => {
  console.log("file", file);
  if (file && file.length) {
    formState.fileUrl = file[0].url;
    formState.fileName = file[0].name;
    formState.fileLength = file[0].fileLength;
  } else {
    formState.fileUrl = undefined;
    formState.fileName = undefined;
    formState.fileLength = undefined;
  }
  console.log("formState", formState);
  formRef.value.validate("fileUrl");
};

const save = () => {
  console.log("formState", formState);
  formRef.value.validate().then(() => {
    const requestUrl = formState.id ? postFirmwareUpdate : postFirmwareAdd;
    requestUrl({ ...formState }).then(() => {
      message.success(formState.id ? "编辑成功" : "新增成功");
      router.go(-1);
    });
  });
};
</script>

<style lang="less" scoped>
.btn-box {
  margin-top: 24px;
  margin-left: 130px;
  :deep(.ant-btn) {
    margin-right: 16px;
  }
}
</style>
